trigger HCPEducationCreate on HCPEducation__c (before delete, after insert, after update) {
    Map<String, Id> regionowner = new Map<String, Id>();
    Map<String, Id> regionid = new Map<String, Id>();

   User defaultU ;
   defaultU = [Select id From User Where UserRole.Name =: 'MSFE: MSFE Sup, Sales Engineer' limit 1];
    //recordtype rt=[select id from recordtype where sobjecttype=:'hcpeducation__c' and name=:'全国医护人员教育活动'];
       if(system.trigger.isupdate||system.trigger.isinsert){
     //  system.assert(trigger.new[0]==null,trigger.new.size());
     boolean b=false;
     if(system.trigger.isupdate && trigger.new.size()>0){
         b=system.trigger.oldmap.get(trigger.new[0].id).NOC__c!=trigger.new[0].NOC__c || 
           system.trigger.oldmap.get(trigger.new[0].id).SOC__c!=trigger.new[0].SOC__c || 
           system.trigger.oldmap.get(trigger.new[0].id).EOC__c!=trigger.new[0].EOC__c || 
           system.trigger.oldmap.get(trigger.new[0].id).WOC__c!=trigger.new[0].WOC__c;
     }
     if(system.trigger.isinsert && trigger.new[0].HCPeducationactivitytype__c!='科内会'){
         b=true;
     }
     
       if(b ){
    for(Place__c p : [select Name, OwnerId from Place__c where PlaceType__c =: 'Region'] ){
        if(p.Name == '华东区' || p.Name == 'EOC'){
            regionowner.put('EOC', p.OwnerId);
            regionid.put('EOC', p.Id);
        }
        else if(p.Name == '华北区' || p.Name == 'NOC'){
            regionowner.put('NOC', p.OwnerId);
            regionid.put('NOC', p.Id);
        }
        else if(p.Name == '华南区' || p.Name == 'SOC'){
            regionowner.put('SOC', p.OwnerId);
            regionid.put('SOC', p.Id);
        }
        else if(p.Name == '华西区' || p.Name == 'WOC'){
            regionowner.put('WOC', p.OwnerId);
            regionid.put('WOC', p.Id);
        }
    }
    
    userrole wocrole=[select id from userrole where rollupdescription=:'MMKT:WOC-Senior Regional MMKT Mgr'];
    userrole eocrole=[select id from userrole where rollupdescription=:'MMKT:EOC-Senior Regional MMKT Mgr'];
    userrole nocrole=[select id from userrole where rollupdescription=:'MMKT:NOC-Senior Regional MMKT Mgr'];
    userrole socrole=[select id from userrole where rollupdescription=:'MMKT:SOC-Senior Regional MMKT Mgr'];
    
    List<User> WOC_user_list = [Select Id,Name From User Where UserRoleId =: wocrole.id];//WOC
    User WOC_user = new User();
    if(WOC_user_list.size() > 0)
    {
        WOC_user = WOC_user_list.get(0);     
    }
    else
    {
        WOC_user = null;
    }
    List<User> SOC_user_list = [Select Id,Name From User Where UserRoleId =: socrole.id];//WOC
    User SOC_user = new User();
    if(SOC_user_list.size() > 0)
    {
        SOC_user = SOC_user_list.get(0);     
    }
    else
    {
        SOC_user = null;
    }   
    List<User> NOC_user_list = [Select Id,Name From User Where UserRoleId =: nocrole.id];//WOC
    User NOC_user = new User();
    if(NOC_user_list.size() > 0)
    {
        NOC_user = NOC_user_list.get(0);     
    }
    else
    {
        NOC_user = null;
    }    
    List<User> EOC_user_list = [Select Id,Name From User Where UserRoleId =: eocrole.id];//WOC
    User EOC_user = new User();
    if(EOC_user_list.size() > 0)
    {
        EOC_user = EOC_user_list.get(0);     
    }
    else
    {
        EOC_user = null;
    }
    
    Boolean WOC_flag = false;
    Boolean EOC_flag = false;
    Boolean NOC_flag = false;
    Boolean SOC_flag = false;
    recordtype rt=[select id from recordtype where sobjecttype=:'HCPEducation__c' and name=:'全国医护人员教育活动'];
   
    if(System.Trigger.isInsert || System.Trigger.isUpdate)
           for(HCPEducation__c edu : Trigger.new){ 
            if(System.Trigger.IsInsert){
                if(edu.RecordTypeId == rt.id){
                    
                    if(edu.EOC__c > 0){
                        if(EOC_user == null){
                            insert new RegionNameList__c(Name = '华东区名单',
                                                     region__c = regionid.get('EOC'),
                                                     TargetNo__c = edu.EOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = defaultU.id);                           
                        }
                     else{
                            insert new RegionNameList__c(Name = '华东区名单',
                                                     region__c = regionid.get('EOC'),
                                                     TargetNo__c = edu.EOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = EOC_user.Id);
                     }
                                                     
                    }
                    if(edu.SOC__c > 0){
                        if(SOC_user == null){
                            insert new RegionNameList__c(Name = '华南区名单',
                                                     region__c = regionid.get('SOC'),
                                                     TargetNo__c = edu.SOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = defaultU.id);
                        }
                        else{                    
                            insert new RegionNameList__c(Name = '华南区名单',
                                                     region__c = regionid.get('SOC'),
                                                     TargetNo__c = edu.SOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = SOC_user.Id);
                                                    
                       }
                    }
                    if(edu.NOC__c > 0){
                        if(NOC_user == null){
                            insert new RegionNameList__c(Name = '华北区名单',
                                                     region__c = regionid.get('NOC'),
                                                     TargetNo__c = edu.NOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = defaultU.id);
                        } 
                        else{                    
                            insert new RegionNameList__c(Name = '华北区名单',
                                                     region__c = regionid.get('NOC'),
                                                     TargetNo__c = edu.NOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = NOC_user.Id);
                                                    
                        }
                    }
                    if(edu.WOC__c > 0){
                        if(WOC_user == null){
                            insert new RegionNameList__c(Name = '华西区名单',
                                                     region__c = regionid.get('WOC'),
                                                     TargetNo__c = edu.WOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = defaultU.id);
                        }
                        else{                      
                            insert new RegionNameList__c(Name = '华西区名单',
                                                     region__c = regionid.get('WOC'),
                                                     TargetNo__c = edu.WOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = WOC_user.Id);
                                                     
                       }
                    }                                 
                }
            }
            else if(System.Trigger.IsUpdate){
                if(edu.RecordTypeId == rt.id){
                    for(RegionNameList__c rnl : [select TargetNo__c, region__c from RegionNameList__c where HCPEducation__c =: edu.Id] )
                    {
                        if(rnl.region__c == regionid.get('EOC')){
                            rnl.TargetNo__c = edu.EOC__c;
                            EOC_flag = true;
                        }    
                        else if(rnl.region__c == regionid.get('NOC')){
                            NOC_flag = true;
                            rnl.TargetNo__c = edu.NOC__c;
                        }    
                        else if(rnl.region__c == regionid.get('WOC')){
                            WOC_flag = true;
                            rnl.TargetNo__c = edu.WOC__c;
                        }    
                        else if(rnl.region__c == regionid.get('SOC')){
                            SOC_flag = true;
                            rnl.TargetNo__c = edu.SOC__c;
                        }
                        update rnl;
                    }
                    if(SOC_flag == false)
                    {
                    if(edu.SOC__c > 0){
                        if(SOC_user == null){
                            insert new RegionNameList__c(Name = '华南区名单',
                                                     region__c = regionid.get('SOC'),
                                                     TargetNo__c = edu.SOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = defaultU.id);
                        }
                        else{                    
                            insert new RegionNameList__c(Name = '华南区名单',
                                                     region__c = regionid.get('SOC'),
                                                     TargetNo__c = edu.SOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = SOC_user.Id);
                                                    
                       }
                    }                   
                    }
                    if(NOC_flag == false)
                    {
                    if(edu.NOC__c > 0){
                        if(NOC_user == null){
                            insert new RegionNameList__c(Name = '华北区名单',
                                                     region__c = regionid.get('NOC'),
                                                     TargetNo__c = edu.NOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = defaultU.id);
                        } 
                        else{                    
                            insert new RegionNameList__c(Name = '华北区名单',
                                                     region__c = regionid.get('NOC'),
                                                     TargetNo__c = edu.NOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = NOC_user.Id);
                                                    
                        }
                    }                   
                    }
                    if(WOC_flag == false)
                    {
                    if(edu.WOC__c > 0){
                        if(WOC_user == null){
                            insert new RegionNameList__c(Name = '华西区名单',
                                                     region__c = regionid.get('WOC'),
                                                     TargetNo__c = edu.WOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = defaultU.id);
                        }
                        else{                      
                            insert new RegionNameList__c(Name = '华西区名单',
                                                     region__c = regionid.get('WOC'),
                                                     TargetNo__c = edu.WOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = WOC_user.Id);
                                                     
                       }
                    }              
                    }
                    if(EOC_flag == false)
                    {
                    if(edu.EOC__c > 0){
                        if(EOC_user == null){
                            insert new RegionNameList__c(Name = '华东区名单',
                                                     region__c = regionid.get('EOC'),
                                                     TargetNo__c = edu.EOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = defaultU.id);                           
                        }
                     else{
                            insert new RegionNameList__c(Name = '华东区名单',
                                                     region__c = regionid.get('EOC'),
                                                     TargetNo__c = edu.EOC__c,
                                                     HCPEducation__c = edu.Id,
                                                     OwnerId  = EOC_user.Id);
                     }
                                                     
                    }          
                    }
                }
            }  
            }     
       } 
       }
    if(System.Trigger.isDelete){
            list<regionnamelist__c> rnl=[select HCPEducation__c from RegionNameList__c where HCPEducation__c IN :Trigger.oldMap.keySet()];
            if(rnl.size()>0){
                delete rnl;
            }
           
         
    } 
     
}